package com.yuxi.common.html;

import lombok.Data;
import org.springframework.ui.Model;
import org.thymeleaf.context.Context;
import com.yuxi.common.SkList;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sunke
 * @version 1.0
 * @date 2021-07-19 10:46
 */
@Data
public class SkContextModel implements Serializable {

    private Context context;
    private String templateName;
    private String htmlName;
    private Model model;

    private boolean createHtmlFlag=true;

    private List<String> htmlList;

    private String lang;

    public SkContextModel() {
        this.context = new Context();
    }



    public SkContextModel setHtmlName(String htmlName) {
        this.htmlName = htmlName;
        addHtml(htmlName);
        return this;
    }

    public SkContextModel addHtml(String html) {
        if (htmlList == null) {
            htmlList = new SkList<>();
        }
        htmlList.add(html);
        return this;
    }

    public List<String> getHtmlList() {
        if (htmlList == null) {
            return htmlList;
        }
        htmlList = htmlList.stream().distinct().collect(Collectors.toList());
        return htmlList;
    }


    public void addAttribute(String attributeName, Object attributeValue) {
        context.setVariable(attributeName, attributeValue);
        if (model != null) {
            model.addAttribute(attributeName, attributeValue);
        }
    }

}
